# Maintainer: Jon Turney <jon.turney@dronecode.org.uk>
# Contributor: Renato Silva <br.renatosilva@gmail.com>
# Contributor: Alexey Pavlov <alexpux@gmail.com>

_realname=breakpad
pkgbase=mingw-w64-${_realname}-git
pkgname=(${MINGW_PACKAGE_PREFIX}-${_realname}-git)
provides=(${MINGW_PACKAGE_PREFIX}-${_realname})
conflicts=(${MINGW_PACKAGE_PREFIX}-${_realname})
replaces=(${MINGW_PACKAGE_PREFIX}-${_realname}-svn)
pkgver=r1680.70914b2d
pkgrel=1
url='https://code.google.com/p/google-breakpad'
pkgdesc='An open-source multi-platform crash reporting system (mingw-w64)'
license=('BSD')
arch=('any')
depends=(${MINGW_PACKAGE_PREFIX}-gcc-libs)
makedepends=(${MINGW_PACKAGE_PREFIX}-curl rsync git gyp)
source=(${_realname}.git::"git+https://chromium.googlesource.com/breakpad/breakpad"
        0002-Remove-local-copy-of-curl-headers.patch
        0003-Link-minidump_upload-and-sym_upload-with-curl.patch
        0004-Build-minidump_upload-and-sym_upload-for-all-targets.patch
        0005-Add-support-for-DWARF-in-PECOFF-as-used-by-Cygwin-an.patch
        0006-Fix-building-minidump-processor-for-MinGW.patch
        0007-Build-PECOFF-DWARF-dump_syms-for-MinGW.patch
        0008-Fix-building-client-library-and-upload-tools-for-Min.patch
        0009-Fix-common.gypi-to-allow-gyp-to-generate-Makefiles-o.patch
        0010-Teach-gyp-generated-Makefile-to-build-crash_generati.patch
        0011-Default-debug_file-to-the-code_file-basename.patch
        0012-Fix-Windows-client-unit-tests-with-gcc.patch
        0013-Add-symsrv_convert.patch
        0015-Use-a-default-debug-id-of-all-zeros-when-no-CV-recor.patch
        0016-Use-__USE_MINGW_ANSI_STDIO-for-MinGW-build.patch
        0017-Provide-a-working-strtok_r-for-MinGW.patch
        0019-Fallback-to-synthesizing-a-debug-id-from-version-and.patch
        0020-Fix-typo-in-the-name-of-the-info-entry-containing-up.patch
        0021-Fix-a-missing-r-in-crash_generation_app.patch
        0026-Disable-DwpReader-for-MinGW.patch
        Makefile
        README.sample)

pkgver() {
    cd "${srcdir}/${_realname}.git"
    printf "r%s.%s" "$(git rev-list --count HEAD)" "$(git rev-parse --short HEAD)"
}

prepare() {
    msg2 "Creating patched-source directory"
    [[ -d "${srcdir}/${_realname}.patched" ]] && rm -rf "${srcdir}/${_realname}.patched"
    mkdir -p "${srcdir}/${_realname}.patched"
    cp -rT "${srcdir}/${_realname}.git" "${srcdir}/${_realname}.patched"

    # Apply patches
    cd "${srcdir}/${_realname}.patched"
    for patch in "${srcdir}"/*.patch; do
        msg2 "Applying ${patch} ..."
        patch -p1 -i "${patch}"
    done

    msg2 "Running autoreconf"
    autoreconf -sif
}

build() {
    msg2 "Synchronizing build directory"
    rsync --recursive --times --links "${srcdir}/${_realname}.patched"/* "${srcdir}/${_realname}.build.${CARCH}"

    cd "${srcdir}/${_realname}.build.${CARCH}"
    ./configure \
      --prefix=${MINGW_PREFIX} \
      --build=${MINGW_CHOST} \
      --host=${MINGW_CHOST}

    # use the autotooled makefile to build the minidump processor library and tools
    make CXXFLAGS="-DUNICODE -D_UNICODE -g" AR=${MINGW_PREFIX}/bin/ar RC=${MINGW_PREFIX}/bin/windres

    # use gyp to generate a Makefile to build the Windows client library
    cd "${srcdir}/${_realname}.build.${CARCH}/src/"
    gyp --no-circular-check --depth=. -f make client/windows/breakpad_client.gyp
    make CXXFLAGS="-DUNICODE -D_UNICODE -D_POSIX_C_SOURCE -g" AR=${MINGW_PREFIX}/bin/ar RC=${MINGW_PREFIX}/bin/windres out/Debug/crash_generation_app out/Debug/obj.target/client/windows/sender/libcrash_report_sender.a
}

package() {
    cd "${srcdir}/${_realname}.build.${CARCH}"
    make CXXFLAGS="-DUNICODE -D_UNICODE -g" AR=${MINGW_PREFIX}/bin/ar RC=${MINGW_PREFIX}/bin/windres DESTDIR="${pkgdir}" install

    # Install headers
    mkdir -p "${pkgdir}${MINGW_PREFIX}/include/breakpad"
    cd "${pkgdir}${MINGW_PREFIX}/include/breakpad"

    mkdir -p "${pkgdir}${MINGW_PREFIX}/include/breakpad/common/"
    cp -v ${srcdir}/${_realname}.build.${CARCH}/src/google_breakpad/common/* common/
    cp -v ${srcdir}/${_realname}.build.${CARCH}/src/common/scoped_ptr.h common/

    mkdir -p "${pkgdir}${MINGW_PREFIX}/include/breakpad/common/windows"
    cp -v ${srcdir}/${_realname}.build.${CARCH}/src/common/windows/string_utils-inl.h common/windows/

    mkdir -p "${pkgdir}${MINGW_PREFIX}/include/breakpad/client/windows/crash_generation"
    cp -v ${srcdir}/${_realname}.build.${CARCH}/src/client/windows/crash_generation/client_info.h client/windows/crash_generation/
    cp -v ${srcdir}/${_realname}.build.${CARCH}/src/client/windows/crash_generation/crash_generation_client.h client/windows/crash_generation/
    cp -v ${srcdir}/${_realname}.build.${CARCH}/src/client/windows/crash_generation/crash_generation_server.h client/windows/crash_generation/
    cp -v ${srcdir}/${_realname}.build.${CARCH}/src/client/windows/crash_generation/minidump_generator.h client/windows/crash_generation/

    mkdir -p "${pkgdir}${MINGW_PREFIX}/include/breakpad/client/windows/common"
    cp -v ${srcdir}/${_realname}.build.${CARCH}/src/client/windows/common/ipc_protocol.h client/windows/common/

    mkdir -p "${pkgdir}${MINGW_PREFIX}/include/breakpad/client/windows/handler"
    cp -v ${srcdir}/${_realname}.build.${CARCH}/src/client/windows/handler/exception_handler.h client/windows/handler/

    mkdir -p "${pkgdir}${MINGW_PREFIX}/include/breakpad/client/windows/sender"
    cp -v ${srcdir}/${_realname}.build.${CARCH}/src/client/windows/sender/crash_report_sender.h client/windows/sender/

    # Headers aren't really designed to be installed, so we need to do some
    # rewriting to make them usefully included using "include <breakpad/somepath/someheader>"
    find ${pkgdir} -name *.h -exec sed -i -e's#client/windows/#breakpad/client/windows/#' {} \;
    find ${pkgdir} -name *.h -exec sed -i -e's#common/scoped_ptr#breakpad/common/scoped_ptr#' {} \;
    find ${pkgdir} -name *.h -exec sed -i -e's#common/windows/#breakpad/common/windows/#' {} \;
    find ${pkgdir} -name *.h -exec sed -i -e's#google_breakpad/common/#breakpad/common/#' {} \;

    # Install a single client library
    cd "${pkgdir}${MINGW_PREFIX}/lib"
    local _windir=$(cygpath -wm ${srcdir})
    ${MINGW_PREFIX}/bin/ar -M <<EOF
CREATE libbreakpad_client.a
ADDLIB ${_windir}/${_realname}.build.${CARCH}/src/out/Debug/obj.target/client/windows/crash_generation/libcrash_generation_client.a
ADDLIB ${_windir}/${_realname}.build.${CARCH}/src/out/Debug/obj.target/client/windows/crash_generation/libcrash_generation_server.a
ADDLIB ${_windir}/${_realname}.build.${CARCH}/src/out/Debug/obj.target/client/windows/handler/libexception_handler.a
ADDLIB ${_windir}/${_realname}.build.${CARCH}/src/out/Debug/obj.target/client/windows/libcommon.a
ADDLIB ${_windir}/${_realname}.build.${CARCH}/src/out/Debug/obj.target/client/windows/sender/libcrash_report_sender.a
SAVE
END
EOF

    # Install the pkgconfig file
    cp -v "${srcdir}/${_realname}.build.${CARCH}/breakpad-client.pc" pkgconfig

    # License
    cd "${pkgdir}${MINGW_PREFIX}/share"
    mkdir -p licenses/${_realname}
    mv doc/${_realname}-0.1 doc/${_realname}
    mv -v doc/${_realname}/LICENSE licenses/${_realname}/LICENSE

    # Install crash_generation_app sample source and instructions
    mkdir -p "${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}/sample"
    cd "${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}/sample"
    cp -v ${srcdir}/README.sample .
    cp -v ${srcdir}/Makefile .
    cp -v ${srcdir}/${_realname}.build.${CARCH}/src/client/windows/tests/crash_generation_app/abstract_class.cc .
    cp -v ${srcdir}/${_realname}.build.${CARCH}/src/client/windows/tests/crash_generation_app/abstract_class.h .
    cp -v ${srcdir}/${_realname}.build.${CARCH}/src/client/windows/tests/crash_generation_app/crash_generation_app.cc .
    cp -v ${srcdir}/${_realname}.build.${CARCH}/src/client/windows/tests/crash_generation_app/crash_generation_app.h .
    cp -v ${srcdir}/${_realname}.build.${CARCH}/src/client/windows/tests/crash_generation_app/crash_generation_app.ico .
    cp -v ${srcdir}/${_realname}.build.${CARCH}/src/client/windows/tests/crash_generation_app/resource.h .
    cp -v ${srcdir}/${_realname}.build.${CARCH}/src/client/windows/tests/crash_generation_app/resource.rc .
    cp -v ${srcdir}/${_realname}.build.${CARCH}/src/client/windows/tests/crash_generation_app/small.ico .

    # Rewrite includes in sample for the rewritten installed headers
    sed -i -e's#client/windows/tests/crash_generation_app/##' ${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}/sample/*
    sed -i -e's#client/windows/#breakpad/client/windows/#' ${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}/sample/*
}

sha256sums=('SKIP'
            '17ba761aed181a48304722f5748bf89c8a364a6c5ac08c9fdbf30ffbb6538990'
            'a54fa3045079768a36028d944b9d41e33aa9ec41a121835cdd34970699e28908'
            '5c899b5f14a4ea46923cb0990977b6cc96403dfe9d94c98a6b96e438b40c44ac'
            '1579da7c8ebeab27b9720a059f816dccd1fb93dfa9abf945a9ea8c67babb2e2e'
            'dbd128ad512044b85ad51b4c5aa965c70ae552bd5f385538d05a6555af8e488a'
            '1273991531b15799c685fe36b0fda29de7c1db83928eff54755877683d0922f7'
            'b35fdecbde622b80e125441b2c84169b38b7bb59616ed02a0153c66764048d94'
            '3bd24a6e882575d396f86cc1646428614266cc2076a3c6805615ec495d33b308'
            'b496568219244d3e578dfbb18b307ff1d6d51555043a2059cd82530f6c6584b0'
            'ca97e04f13d606f4195a4c75cf064179baff0f933b88f35f482745f6fa1c7663'
            '1fb7a7817b37fa65bdc0f3504553f00ecc92033f67ae8038723ea624adf83609'
            '78c5f788f3d071878f13f46efacad5d0a14ec751ddb4608fc596db7b50c96568'
            '252eee0e97a9e24559823b4c2a10ec8485d3dab209bb46b1f6708822fc915024'
            '5202e5156a6bef3f5df2bc6ef20d0399e791c1fe892725fe80e0a1a54bf90f72'
            '7b8f7a557d67fb20cc887c8a54ba672d5dee604ae03c2468b8a9f1a2b8858fca'
            '53d7068209a88a174768a5df4e7d3e98991e29ad780f072417b8d07f64423598'
            'e9e29a83e1cc668d6cb6fb72334a4e5d616859eab7f248df636338070440f1b5'
            '45c55049ab28f2a432c2753277f3081e8f940c4b757267ed582ec6be083692ff'
            '0958def790a86057bf8e8bb18040e42f11ee412662aa09fb960b24112569a062'
            'b1a4025d888bcd1d752b3b6c93d6e6ae353eae050197b1ae2c54f9b90bfb1292'
            '0b1421202edef83db517a310824ee1f7680e0a80229f5a63aa60cc2ef6b93b19')
